package com.dianping.cat.consumer.problem.model.transform;

import com.dianping.cat.consumer.problem.model.IEntity;
import com.dianping.cat.consumer.problem.model.entity.Duration;
import com.dianping.cat.consumer.problem.model.entity.Entity;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.GraphTrend;
import com.dianping.cat.consumer.problem.model.entity.JavaThread;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.entity.Segment;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
















public class DefaultSaxParser
  extends DefaultHandler
{
/*  47 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  49 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  51 */   private Stack<String> m_tags = new Stack();

/*  53 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  57 */   private StringBuilder m_text = new StringBuilder();


/*  60 */   public static ProblemReport parse(InputStream in) throws SAXException, IOException { return (ProblemReport)parseEntity(ProblemReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  64 */   public static ProblemReport parse(Reader reader) throws SAXException, IOException { return (ProblemReport)parseEntity(ProblemReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  68 */   public static ProblemReport parse(String xml) throws SAXException, IOException { return (ProblemReport)parseEntity(ProblemReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  74 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  75 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  77 */       factory.setValidating(false);
      factory.setFeature("http://xml.org/sax/features/validation", false);

/*  80 */       factory.newSAXParser().parse(is, handler);
/*  81 */       return (T)handler.getEntity();
/*  82 */     } catch (ParserConfigurationException e) {
/*  83 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  88 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  92 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  98 */     if (obj instanceof String) {
/*  99 */       String str = (String)obj;

/* 101 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/* 102 */         return (T)str.substring(1);
      }
/* 104 */       return obj;
    }
/* 106 */     if (obj instanceof InputStream) {
/* 107 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 109 */       in.mark(3);

/* 111 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 112 */         in.reset();
      }

/* 115 */       return (T)in;
/* 116 */     }  if (obj instanceof Reader) {
/* 117 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 119 */       in.mark(1);

/* 121 */       if (in.read() != 65279) {
/* 122 */         in.reset();
      }

/* 125 */       return (T)in;
    }
/* 127 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 133 */     if (value == null || value.length() == 0) {
/* 134 */       return defaultValue;
    }

/* 137 */     if (type == Boolean.class)
/* 138 */       return (T)Boolean.valueOf(value); 
/* 139 */     if (type == Integer.class)
/* 140 */       return (T)Integer.valueOf(value); 
/* 141 */     if (type == Long.class)
/* 142 */       return (T)Long.valueOf(value); 
/* 143 */     if (type == Short.class)
/* 144 */       return (T)Short.valueOf(value); 
/* 145 */     if (type == Float.class)
/* 146 */       return (T)Float.valueOf(value); 
/* 147 */     if (type == Double.class)
/* 148 */       return (T)Double.valueOf(value); 
/* 149 */     if (type == Byte.class)
/* 150 */       return (T)Byte.valueOf(value); 
/* 151 */     if (type == Character.class) {
/* 152 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 154 */     return (T)value;
  }




/* 160 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 165 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 170 */     if (uri == null || uri.length() == 0) {
/* 171 */       Object currentObj = this.m_objs.pop();
/* 172 */       String currentTag = (String)this.m_tags.pop();

/* 174 */       if (currentObj instanceof ProblemReport) {
/* 175 */         ProblemReport problemReport = (ProblemReport)currentObj;

/* 177 */         if ("domain".equals(currentTag)) {
/* 178 */           problemReport.addDomain(getText());
/* 179 */         } else if ("ip".equals(currentTag)) {
/* 180 */           problemReport.addIp(getText());
        }
/* 182 */       } else if (currentObj instanceof Segment) {
/* 183 */         Segment segment = (Segment)currentObj;

/* 185 */         if ("message".equals(currentTag)) {
/* 186 */           segment.addMessage(getText());
        }
/* 188 */       } else if (currentObj instanceof Duration) {
/* 189 */         Duration duration = (Duration)currentObj;

/* 191 */         if ("message".equals(currentTag)) {
/* 192 */           duration.addMessage(getText());
        }
      }
    }

/* 197 */     this.m_text.setLength(0);
  }


/* 201 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 205 */   protected String getText() { return this.m_text.toString(); }


  private void parseForDuration(Duration parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 209 */     if ("messages".equals(qName) || "message".equals(qName)) {
/* 210 */       this.m_objs.push(parentObj);
    } else {
/* 212 */       throw new SAXException(String.format("Element(%s) is not expected under duration!", new Object[] { qName }));
    }

/* 215 */     this.m_tags.push(qName);
  }

  private void parseForEntity(Entity parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 219 */     if ("thread".equals(qName)) {
/* 220 */       JavaThread thread = this.m_maker.buildThread(attributes);

/* 222 */       this.m_linker.onThread(parentObj, thread);
/* 223 */       this.m_objs.push(thread);
/* 224 */     } else if ("duration".equals(qName)) {
/* 225 */       Duration duration = this.m_maker.buildDuration(attributes);

/* 227 */       this.m_linker.onDuration(parentObj, duration);
/* 228 */       this.m_objs.push(duration);
/* 229 */     } else if ("graph-trend".equals(qName)) {
/* 230 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 232 */       this.m_linker.onGraphTrend(parentObj, graphTrend);
/* 233 */       this.m_objs.push(graphTrend);
    } else {
/* 235 */       throw new SAXException(String.format("Element(%s) is not expected under entity!", new Object[] { qName }));
    }

/* 238 */     this.m_tags.push(qName);
  }

  private void parseForEntry(Entry parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 242 */     if ("duration".equals(qName)) {
/* 243 */       Duration duration = this.m_maker.buildDuration(attributes);

/* 245 */       this.m_linker.onDuration(parentObj, duration);
/* 246 */       this.m_objs.push(duration);
/* 247 */     } else if ("thread".equals(qName)) {
/* 248 */       JavaThread thread = this.m_maker.buildThread(attributes);

/* 250 */       this.m_linker.onThread(parentObj, thread);
/* 251 */       this.m_objs.push(thread);
    } else {
/* 253 */       throw new SAXException(String.format("Element(%s) is not expected under entry!", new Object[] { qName }));
    }

/* 256 */     this.m_tags.push(qName);
  }

  private void parseForGraphTrend(GraphTrend parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 260 */     this.m_objs.push(parentObj);
/* 261 */     this.m_tags.push(qName);
  }

  private void parseForMachine(Machine parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 265 */     if ("entry".equals(qName)) {
/* 266 */       Entry entry = this.m_maker.buildEntry(attributes);

/* 268 */       this.m_linker.onEntry(parentObj, entry);
/* 269 */       this.m_objs.push(entry);
/* 270 */     } else if ("entity".equals(qName)) {
/* 271 */       Entity entity = this.m_maker.buildEntity(attributes);

/* 273 */       this.m_linker.onEntity(parentObj, entity);
/* 274 */       this.m_objs.push(entity);
    } else {
/* 276 */       throw new SAXException(String.format("Element(%s) is not expected under machine!", new Object[] { qName }));
    }

/* 279 */     this.m_tags.push(qName);
  }

  private void parseForProblemReport(ProblemReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 283 */     if ("domain-names".equals(qName) || "domain".equals(qName) || "ips".equals(qName) || "ip".equals(qName)) {
/* 284 */       this.m_objs.push(parentObj);
/* 285 */     } else if ("machine".equals(qName)) {
/* 286 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 288 */       this.m_linker.onMachine(parentObj, machine);
/* 289 */       this.m_objs.push(machine);
    } else {
/* 291 */       throw new SAXException(String.format("Element(%s) is not expected under problem-report!", new Object[] { qName }));
    }

/* 294 */     this.m_tags.push(qName);
  }

  private void parseForSegment(Segment parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 298 */     if ("messages".equals(qName) || "message".equals(qName)) {
/* 299 */       this.m_objs.push(parentObj);
    } else {
/* 301 */       throw new SAXException(String.format("Element(%s) is not expected under segment!", new Object[] { qName }));
    }

/* 304 */     this.m_tags.push(qName);
  }

  private void parseForThread(JavaThread parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 308 */     if ("segment".equals(qName)) {
/* 309 */       Segment segment = this.m_maker.buildSegment(attributes);

/* 311 */       this.m_linker.onSegment(parentObj, segment);
/* 312 */       this.m_objs.push(segment);
    } else {
/* 314 */       throw new SAXException(String.format("Element(%s) is not expected under thread!", new Object[] { qName }));
    }

/* 317 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 321 */     if ("problem-report".equals(qName)) {
/* 322 */       ProblemReport problemReport = this.m_maker.buildProblemReport(attributes);

/* 324 */       this.m_entity = problemReport;
/* 325 */       this.m_objs.push(problemReport);
/* 326 */       this.m_tags.push(qName);
/* 327 */     } else if ("machine".equals(qName)) {
/* 328 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 330 */       this.m_entity = machine;
/* 331 */       this.m_objs.push(machine);
/* 332 */       this.m_tags.push(qName);
/* 333 */     } else if ("entry".equals(qName)) {
/* 334 */       Entry entry = this.m_maker.buildEntry(attributes);

/* 336 */       this.m_entity = entry;
/* 337 */       this.m_objs.push(entry);
/* 338 */       this.m_tags.push(qName);
/* 339 */     } else if ("thread".equals(qName)) {
/* 340 */       JavaThread thread = this.m_maker.buildThread(attributes);

/* 342 */       this.m_entity = thread;
/* 343 */       this.m_objs.push(thread);
/* 344 */       this.m_tags.push(qName);
/* 345 */     } else if ("segment".equals(qName)) {
/* 346 */       Segment segment = this.m_maker.buildSegment(attributes);

/* 348 */       this.m_entity = segment;
/* 349 */       this.m_objs.push(segment);
/* 350 */       this.m_tags.push(qName);
/* 351 */     } else if ("duration".equals(qName)) {
/* 352 */       Duration duration = this.m_maker.buildDuration(attributes);

/* 354 */       this.m_entity = duration;
/* 355 */       this.m_objs.push(duration);
/* 356 */       this.m_tags.push(qName);
/* 357 */     } else if ("entity".equals(qName)) {
/* 358 */       Entity entity = this.m_maker.buildEntity(attributes);

/* 360 */       this.m_entity = entity;
/* 361 */       this.m_objs.push(entity);
/* 362 */       this.m_tags.push(qName);
/* 363 */     } else if ("graph-trend".equals(qName)) {
/* 364 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 366 */       this.m_entity = graphTrend;
/* 367 */       this.m_objs.push(graphTrend);
/* 368 */       this.m_tags.push(qName);
    } else {
/* 370 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 376 */     if (uri == null || uri.length() == 0) {
/* 377 */       if (this.m_objs.isEmpty()) {
/* 378 */         parseRoot(qName, attributes);
      } else {
/* 380 */         Object parent = this.m_objs.peek();
/* 381 */         String tag = (String)this.m_tags.peek();

/* 383 */         if (parent instanceof ProblemReport) {
/* 384 */           parseForProblemReport((ProblemReport)parent, tag, qName, attributes);
/* 385 */         } else if (parent instanceof Machine) {
/* 386 */           parseForMachine((Machine)parent, tag, qName, attributes);
/* 387 */         } else if (parent instanceof Entry) {
/* 388 */           parseForEntry((Entry)parent, tag, qName, attributes);
/* 389 */         } else if (parent instanceof JavaThread) {
/* 390 */           parseForThread((JavaThread)parent, tag, qName, attributes);
/* 391 */         } else if (parent instanceof Segment) {
/* 392 */           parseForSegment((Segment)parent, tag, qName, attributes);
/* 393 */         } else if (parent instanceof Duration) {
/* 394 */           parseForDuration((Duration)parent, tag, qName, attributes);
/* 395 */         } else if (parent instanceof Entity) {
/* 396 */           parseForEntity((Entity)parent, tag, qName, attributes);
/* 397 */         } else if (parent instanceof GraphTrend) {
/* 398 */           parseForGraphTrend((GraphTrend)parent, tag, qName, attributes);
        } else {
/* 400 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 404 */       this.m_text.setLength(0);
    } else {
/* 406 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 412 */       return (new SimpleDateFormat(format)).parse(str);
/* 413 */     } catch (ParseException e) {
/* 414 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/problem/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */